package com.orange.common.util

/**
 * Created by Orange on 2022/11/15 20:42.
 * 基于moshi的json转换封装
 * https://blog.csdn.net/jingzz1/article/details/107315110
 */
//object MoshiUtils {
//
//    val moshiBuild: Moshi = Moshi.Builder().build()
//    //使用Kotlin-Reflect包时，这里改一下:
//    //val moshiBuild = Moshi.Builder().add(KotlinJsonAdapterFactory()).build()
//
//
//    //普通序列化
//    fun <T> fromJson(json: String, type: Type): T? = getAdapter<T>(type).fromJson(json)
//    fun <T> fromJons(buffer: BufferedSource, type: Type): T? = getAdapter<T>(type).fromJson(buffer)
//    fun <T> fromJons(`is`: InputStream, type: Type): T? = getAdapter<T>(type).fromJson(Buffer().readFrom(`is`))
//    fun <T> fromJons(reader: JsonReader, type: Type): T? = getAdapter<T>(type).fromJson(reader)
//
//    //自动获取type序列化,性能较差
//    inline fun <reified T:Any> fromJson(json: String): T? = getAdapter<T>().fromJson(json)
//    inline fun <reified T> fromJson(buffer: BufferedSource): T? = getAdapter<T>().fromJson(buffer)
//    inline fun <reified T> fromJson(`is`: InputStream): T? = getAdapter<T>().fromJson(Buffer().readFrom(`is`))
//    inline fun <reified T> fromJson(reader: JsonReader): T? = getAdapter<T>().fromJson(reader)
//
//    //高效序列化为list
//    inline fun <reified T> listFromJson(json: String): MutableList<T> =
//        fromJson(json, Types.newParameterizedType(MutableList::class.java, T::class.java))
//            ?: mutableListOf()
//
//    inline fun <reified T> listFromJson(buffer: BufferedSource): MutableList<T> =
//        fromJons(buffer, Types.newParameterizedType(MutableList::class.java, T::class.java))
//            ?: mutableListOf()
//
//    inline fun <reified T> listFromJson(`is`: InputStream): MutableList<T> =
//        fromJons(`is`, Types.newParameterizedType(MutableList::class.java, T::class.java))
//            ?: mutableListOf()
//
//    inline fun <reified T> listFromJson(reader: JsonReader): MutableList<T> =
//        fromJons(reader, Types.newParameterizedType(MutableList::class.java, T::class.java))
//            ?: mutableListOf()
//
//
//    //高效序列化为map
//    inline fun <reified K, reified V> mapFromJson(json: String): MutableMap<K, V> = fromJson(
//        json,
//        Types.newParameterizedType(MutableMap::class.java, K::class.java, V::class.java)
//    ) ?: mutableMapOf()
//
//    inline fun <reified K, reified V> mapFromJson(buffer: BufferedSource): MutableMap<K, V> =
//        fromJons(
//            buffer,
//            Types.newParameterizedType(MutableMap::class.java, K::class.java, V::class.java)
//        )?: mutableMapOf()
//
//    inline fun <reified K, reified V> mapFromJson(`is`: InputStream): MutableMap<K, V> = fromJons(
//        `is`,
//        Types.newParameterizedType(MutableMap::class.java, K::class.java, V::class.java)
//    )?: mutableMapOf()
//
//    inline fun <reified K, reified V> mapFromJson(reader: JsonReader): MutableMap<K, V> = fromJons(
//        reader,
//        Types.newParameterizedType(MutableMap::class.java, K::class.java, V::class.java)
//    )?: mutableMapOf()
//    //反序列化
//    inline fun <reified T> toJson(t: T?) = getAdapter<T>().toJson(t) ?: ""
//
//    fun <T> getAdapter(type: Type): JsonAdapter<T> = moshiBuild.adapter(type)
//    inline fun <reified T> getAdapter(): JsonAdapter<T> =  moshiBuild.adapter(object : TypeToken<T>(){}.type)
//
//}
//
//abstract class TypeToken<T>{
//    val type:Type get() = run{
//        val superclass = javaClass.genericSuperclass
//        Util.canonicalize((superclass as ParameterizedType).actualTypeArguments[0])
//    }
//}

//    反射方法 来自 https://blog.csdn.net/yuzhiqiang_1993/article/details/125132064
//    abstract class MoshiTypeReference<T> // 自定义的类，用来包装泛型
//
//    val moshi: Moshi = Moshi.Builder()
//        .addLast(KotlinJsonAdapterFactory()).build()
//
//    inline fun <reified T> toJson(src: T, indent: String = ""): String {
//        try {
//            val jsonAdapter = moshi.adapter<T>(getGenericType<T>())
//            return jsonAdapter.indent(indent).toJson(src)
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
//        return ""
//    }
//
//    inline fun <reified T> fromJson(jsonStr: String): T? {
//        try {
//            val jsonAdapter = moshi.adapter<T>(getGenericType<T>())
//            return jsonAdapter.fromJson(jsonStr)
//        } catch (e: Exception) {
//            e.printStackTrace()
//        }
//        return null
//    }
//
//    inline fun <reified T> getGenericType(): Type {
//        return object :
//            MoshiTypeReference<T>() {}::class.java
//            .genericSuperclass
//            .let { it as ParameterizedType }
//            .actualTypeArguments
//            .first()
//    }